package com.code2roc.jarmanage.util;

import com.code2roc.jarmanage.depoly.DepolyPlatform;
import com.code2roc.jarmanage.depoly.DepolyStatus;
import com.code2roc.jarmanage.model.ServiceInfo;
import oshi.SystemInfo;
import oshi.software.os.OSService;
import oshi.software.os.OperatingSystem;
import oshi.software.os.linux.LinuxOperatingSystem;
import oshi.software.os.windows.WindowsOperatingSystem;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class ServiceUtil {

    public static List<ServiceInfo> getServiceInfoList(){
        List<ServiceInfo> serviceInfoList = new ArrayList<>();
        if(EnviromentUtil.getDepolyPlaform().equals(DepolyPlatform.Windows)){
            SystemInfo si = new SystemInfo();
            OperatingSystem os = si.getOperatingSystem();
            List<OSService> services = os.getServices();
            for (OSService item:services) {
                serviceInfoList.add(new ServiceInfo(item.getName(),item.getState().name()));
            }
        }else{
            try{
                String command = "systemctl list-units --all";
                String commandResultFilePath = CMDUtil.executeLinuxCommandWithStore(command);
                BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(commandResultFilePath)));
                String line = reader.readLine();
                while (line != null) {
                    String[] data = line.trim().split("\\s+");
                    if(data.length>=4){
                        String unitName = data[0];
                        String status = data[3];
                        if(unitName.endsWith(".service")) {
                            // 获取服务名称和状态
                            String serviceName = unitName.substring(0, unitName.length() - 8);
                            String serviceStatus = OSService.State.STOPPED.name();
                            if(status.equals("running")){
                                serviceStatus= OSService.State.RUNNING.name();
                            }
                            serviceInfoList.add(new ServiceInfo(serviceName,serviceStatus));
                        }
                    }
                    line = reader.readLine();
                }
            }catch (Exception e){
                LogUtil.error(e);
            }
        }
        return serviceInfoList;
    }

    public static void startService(String serviceName){
        if(EnviromentUtil.getDepolyPlaform().equals(DepolyPlatform.Windows)){
            String command = "net start " + serviceName;
            CMDUtil.executeWindowCommand(command);
        }else{
            String command = "systemctl reset-failed";
            CMDUtil.executeLinuxCommand(command);
            command = "systemctl start " + serviceName;
            CMDUtil.executeLinuxCommand(command);
        }
    }

    public static void stopService(String serviceName){
        if(EnviromentUtil.getDepolyPlaform().equals(DepolyPlatform.Windows)){
            String command = "net stop  " + serviceName;
            CMDUtil.executeWindowCommand(command);
        }else{
            String command = "systemctl reset-failed";
            CMDUtil.executeLinuxCommand(command);
            command = "systemctl stop " + serviceName;
            CMDUtil.executeLinuxCommand(command);
        }
    }
}
